<template>
	<view class="content">
		<!-- 头部logo -->
		<view class="header">
			<image :src="logoImage"></image>
		</view>
		<!-- 主体 -->
		<view class="main">
			<wInput
					v-model="phoneData"
					type="text"
					maxlength="11"
					placeholder="手机号"
				></wInput>
				<wInput
					v-model="passData"
					type="password"
					maxlength="11"
					placeholder="登录密码"
					isShowPass
				></wInput>
				<wInput
					v-model="verCode"
					type="number"
					maxlength="6"
					placeholder="验证码"
					isShowCode
					ref="runCode"
					@setCode="getVerCode()"
				></wInput>
			</view>
			
			<wButton 
				text="注 册"
				:rotate="isRotate" 
				@click.native="startReg()"
			></wButton>
		<!-- 底部信息 -->
		<!-- <view class="footer">
			<text 
				@tap="isShowAgree" 
				class="iconfont t"
				:class="showAgree?'icon-danxuan':'icon-circle'"
			>同意</text>
			<navigator url="" open-type="navigate">《乐字节用户协议》</navigator>
		</view> -->
	</view>
</template>

<script>
	var _this;
	import wInput from '../../components/watch-login/watch-input.vue' //input
	import wButton from '../../components/watch-login/watch-button.vue' //button
	
	export default {
		data() {
			return {
				//logo图片 base64
				logoImage:'/static/logo.jpg',
				phoneData:'', // 用户/电话
				passData:'', //密码
				verCode:"", //验证码
				showAgree:true, //协议是否选择
				isRotate: false, //是否加载旋转
				isSendCode: false, //是否发送验证码
			}
		},
		components:{
			wInput,
			wButton,
		},
		mounted() {
			_this= this;
		},
		methods: {
			isShowAgree(){
				//是否选择协议
				_this.showAgree = !_this.showAgree;
			},
			getVerCode(){
				if(this.isSendCode){
					uni.showToast({
					    icon: 'none',
						position: 'bottom',
					    title: '短信发送过于频繁'
					});
					return ;
				}
				//获取验证码
				var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;  
				if (this.phoneData.length != 11 || !myreg.test(this.phoneData)) {
				     uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
				
				this.$api.get('/sms/send_register_msg',{
					smsSendType: 1,
					mobilePhone: this.phoneData,
				}).then(r=>{
					this.$refs.runCode.$emit('runCode');
					this.isSendCode = true;
					setTimeout(function(){
						_this.$refs.runCode.$emit('runCode',0); //假装模拟下需要 终止倒计时
						_this.isSendCode = false;
					},60000)
				})
			},
		    startReg() {
				//注册
				if(this.isRotate){
					//判断是否加载中，避免重复点击请求
					return false;
				}
				if (this.showAgree == false) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '请先同意《协议》'
				    });
				    return false;
				}
				var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;  
				if (this.phoneData.length != 11 || !myreg.test(this.phoneData)) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '手机号不正确'
				    });
				    return false;
				}
		        if (this.passData.length < 6) {
		            uni.showToast({
		                icon: 'none',
						position: 'bottom',
		                title: '密码不正确'
		            });
		            return false;
		        }
				if (this.verCode.length != 6) {
				    uni.showToast({
				        icon: 'none',
						position: 'bottom',
				        title: '验证码不正确'
				    });
				    return false;
				}
				console.log("注册成功")
				_this.isRotate=true;
				let shareCode = uni.getStorageSync('shareCode');
				this.$api.post('/user/reg',{
					phone: this.phoneData,
					loginPassword: this.passData,
					captcha: this.verCode,
					shareCode: shareCode,
				},{
					'Content-Type': 'application/x-www-form-urlencoded'
				}).then(r=>{
					_this.isRotate=false;
					if(r.data.code == 200){
						uni.showToast({
						    icon: 'none',
							position: 'bottom',
						    title: '注册成功'
						});
						setTimeout(function(){
							uni.navigateTo({  
								url: '/pages/user/login'  
							})
						}, 2000);
					}else{
						uni.showToast({
						    icon: 'none',
							position: 'bottom',
						    title: '注册失败'
						});
					}
				});
				
				
				setTimeout(function(){
					_this.isRotate=false
				},3000)
		    }
		}
	}
</script>

<style>
	page{overflow: hidden;}
	/* 头部 logo */
	.header {
		width:161upx;
		height:161upx;
		box-shadow:0upx 0upx 60upx 0upx rgba(0,0,0,0.1);
		border-radius:50%;
		margin-top: 80upx;
		margin-bottom: 50upx;
		margin-left: auto;
		margin-right: auto;
	}
	.header image{
		width:161upx;
		height:161upx;
		border-radius:50%;
	}
	/* 主体 */
	.main {
		display: flex;
		flex-direction: column;
		padding-left: 70upx;
		padding-right: 70upx;
	}
	.tips {
		color: #999999;
		font-size: 28upx;
		margin-top: 64upx;
		margin-left: 48upx;
	}
	
	/* 其他登录方式 */
	.other_login{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-top: 256upx;
		text-align: center;
	}
	.login_icon{
		border: none;
		font-size: 64upx;
		margin: 0 64upx 0 64upx;
		color: rgba(0,0,0,0.7)
	}
	.wechat_color{
		color: #83DC42;
	}
	.weibo_color{
		color: #F9221D;
	}
	.github_color{
		color: #24292E;
	}
	
	/* 底部 */
	.footer{
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		font-size: 28upx;
		margin-top: 64upx;
		color: rgba(0,0,0,0.7);
		text-align: center;
		height: 40upx;
		line-height: 40upx;
	}
	.footer .t{
		margin-top: 3upx;
	}
	.footer .line{font-size: 24upx;
	margin-left: 15upx;
	margin-right: 15upx;}
</style>